/**
 * Copyright (c) 2017-2018, zengjintao (1913188966@qq.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fast.framework.web.orm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fast.framework.web.orm.config.DataSourceManager;

public class JdbcTemplate {

	static final Object[] NULL_PARA_ARRAY = new Object[0];
	
	private static final ModelBuilder modelBuilder = ModelBuilder.me();
	
	public <T> List<T> findAll(String sql,Class<T> cla,Object...  paras) throws SQLException{
		Connection connection = DataSourceManager.getDbConfig().getConnection();
		return find(connection, sql, cla, paras);
	}
	
	public <T> List<T> findAll(String sql,Class<T> cla){
		Connection connection;
		try {
			connection = DataSourceManager.getDbConfig().getConnection();
			return find(connection, sql,cla); 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private  <T> List<T> find(Connection connection, String sql,Class<T> cla) {
		PreparedStatement preparedStatement;
		try {
			preparedStatement = connection.prepareStatement(sql);
			ResultSet resultSet = preparedStatement.executeQuery();
		    List<T> modelList = modelBuilder.builderModelList(resultSet,cla);
			connection.close();
			return modelList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public  <T> T findOne(String sql,Class<T> cla){
		List<T> result;
		try {
			result = findAll(sql, cla,NULL_PARA_ARRAY);
			return result.size()> 0 ? result.get(0) : null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null; 
	}
	
	public  <T> T findOne(String sql,Class<T> cla,Object... paras) {
		List<T> result;
		try {
			result = findAll(sql, cla,paras);
			return result.size()> 0 ? result.get(0) : null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null; 
		
	}
	
	private <T> List<T> find(Connection connection, String sql,Class<T> cla,Object... paras) {
		try {
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			modelBuilder.fillStatement(preparedStatement,sql,paras);
			ResultSet resultSet = preparedStatement.executeQuery();
			List<T> modelList = modelBuilder.builderModelList(resultSet,cla);
			connection.close();
			return modelList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
